package com.zy.web.controller.business;

import com.zy.business.domain.TbDailyReport;
import com.zy.business.domain.TbDailyReportAnnotate;
import com.zy.business.domain.TbUserDailyReport;
import com.zy.business.service.DailyReportService;
import com.zy.common.annotation.Log;
import com.zy.common.annotation.RepeatSubmit;
import com.zy.common.core.controller.BaseController;
import com.zy.common.core.domain.AjaxResult;
import com.zy.common.core.domain.entity.SysUser;
import com.zy.common.core.page.TableDataInfo;
import com.zy.common.enums.BusinessType;
import com.zy.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @description: 日报管理控制器
 * @author: whf
 * @create: 2023-01-13 09:42
 **/
@Api(tags = "日报管理接口")
@RestController
@RequestMapping("/dailyReport")
public class DailyReportController extends BaseController {

    @Autowired
    private DailyReportService dailyReportService;

    /**
     * 获取用户列表
     */
    @ApiOperation("获取成员日报列表")
    @GetMapping("/list")
    public AjaxResult list(SysUser user,String reportDate)
    {
        List<TbUserDailyReport> list = dailyReportService.selectReportList(user,reportDate);
        return AjaxResult.success(list);
    }

    @ApiOperation("根据日期获取日报信息")
    @GetMapping("/getReportByDate")
    public AjaxResult getReportByDate(Long userId,@RequestParam String date) {
        return dailyReportService.getReportByDate(userId, date);
    }

    @ApiOperation(value = "获取我的日报数据")
    @GetMapping("/getMyData")
    public AjaxResult getUserMonthData(String month){
        List<Map<String, Object>> mapList = dailyReportService.selectMyData(month);
        return AjaxResult.success(mapList);
    }

    @ApiOperation(value = "添加日报")
    @Log(title = "日报管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @RepeatSubmit
    public AjaxResult add(@RequestBody TbUserDailyReport userDailyReport){
        return dailyReportService.addDailyReport(userDailyReport);
    }

    @ApiOperation("修改日报")
    @Log(title = "日报管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody TbUserDailyReport userDailyReport){
        return dailyReportService.editDailyReport(userDailyReport);
    }

    @ApiOperation(value = "导出日报")
    @PostMapping("/export")
    public void exportWord(SysUser user,String reportDate, HttpServletRequest request, HttpServletResponse response){
        dailyReportService.exportWord(user,reportDate,request,response);
    }

    @ApiOperation(value = "获取日报统计列表")
    @GetMapping( "/reportList")
    public TableDataInfo reportList(Long deptId,String month) {
        return dailyReportService.reportList(deptId,month);
    }

    @ApiOperation(value = "根据项目id和用户名获取日报列表")
    @GetMapping("/getReportListByProject")
    public TableDataInfo getReportListByProject(@RequestParam String userName,@RequestParam Long projectId,String reportDate){
        startPage();
        List<TbDailyReport> list = dailyReportService.getReportListByProject(userName,projectId,reportDate);
        return getDataTable(list);
    }

    @ApiOperation(value = "修改状态为已读")
    @PostMapping("/changeView/{id}")
    public AjaxResult changeView(@PathVariable Long id){
        return dailyReportService.changeView(id);
    }

    @ApiOperation(value = "保存批注")
    @PostMapping("/saveComments")
    public AjaxResult saveComments(@RequestBody TbDailyReportAnnotate annotate){
        annotate.setAnnotateUser(SecurityUtils.getUsername());
        return toAjax(dailyReportService.saveComments(annotate));
    }

    @ApiOperation(value = "修改批注")
    @PostMapping("/editComments")
    public AjaxResult editComments(@RequestBody TbDailyReportAnnotate annotate){
        return toAjax(dailyReportService.editComments(annotate));
    }

    @ApiOperation(value = "手机端获取我的日报列表数据")
    @GetMapping("/getMyReportListByDate")
    public TableDataInfo getMyReportListByDate(String startDate,String endDate){
        startPage();
        List<TbUserDailyReport> dailyReportList = dailyReportService.getMyReportListByDate(startDate,endDate);
        return getDataTable(dailyReportList);
    }

    @ApiOperation(value = "手机端点击查看日报详情")
    @GetMapping("/getDetail")
    public AjaxResult getDetail(Long reportId){
        return dailyReportService.getDetail(reportId);
    }

    @ApiOperation(value = "验证所选日期的日报是否存在")
    @PostMapping("/verify")
    public AjaxResult verify(@RequestParam String reportDate){
        Boolean flag = dailyReportService.verify(reportDate);
        return AjaxResult.success().put("flag",flag);
    }

}
